//
//  ComposeToolbar.m
//  SinaWebo
//
//  Created by Pink Elephant on 15/4/16.
//  Copyright (c) 2015年 亦心. All rights reserved.
//

#import "ComposeToolbar.h"

@implementation ComposeToolbar
+(ComposeToolbar *)composeToolBar{
    return [[self alloc]init];
}
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
    }
    return self;
}

-(void)setupView{
    // 1.设置背景
    self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"compose_toolbar_background_os7"]];
    // 2.添加按钮
    [self addButtonWithIcon:@"compose_camerabutton_background_os7" highIcon:@"compose_camerabutton_background_highlighted_os7" tag:ComposeToolbarButtonTypeCamera];
    [self addButtonWithIcon:@"compose_toolbar_picture_os7" highIcon:@"compose_toolbar_picture_highlighted_os7" tag:ComposeToolbarButtonTypePicture];
    [self addButtonWithIcon:@"compose_mentionbutton_background_os7" highIcon:@"compose_mentionbutton_background_highlighted_os7" tag:ComposeToolbarButtonTypeMention];
    [self addButtonWithIcon:@"compose_trendbutton_background_os7" highIcon:@"compose_trendbutton_background_highlighted_os7" tag:ComposeToolbarButtonTypeTrend];
    [self addButtonWithIcon:@"compose_emoticonbutton_background_os7" highIcon:@"compose_emoticonbutton_background_highlighted_os7" tag:ComposeToolbarButtonTypeEmotion];
}

- (void)addButtonWithIcon:(NSString *)icon highIcon:(NSString *)highIcon tag:(int)tag{
    UIButton *button = [[UIButton alloc] init];
    button.tag = tag;
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [button setImage:[UIImage imageNamed:icon] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:highIcon] forState:UIControlStateHighlighted];
    [self addSubview:button];
}

-(void)layoutSubviews{
    [super layoutSubviews];
    CGFloat buttonY=0;
    CGFloat buttonW=SCREEN_PORTRAIT_WIDTH/self.subviews.count;
    CGFloat buttonH=self.frame.size.height;
    for (int index=0; index<self.subviews.count; index++) {
        UIButton *button=self.subviews[index];
        CGFloat buttonX=buttonW*index;
        button.frame=CGRectMake(buttonX, buttonY, buttonW, buttonH);
    }
}

/**
 *  监听按钮点击
 */
- (void)buttonClick:(UIButton *)button{
    if ([self.delegate respondsToSelector:@selector(composeToobar:didClickdButton:)]) {
        [self.delegate composeToobar:self didClickdButton:(int)button.tag];
    }
}

@end
